//**********************************Motor.c***********************************
//Trevor Wood, Matt Villarrubia
//EE345M, Spring 2011
//*****************************************************************************
#include "..\inc/hw_memmap.h"
#include "..\inc/hw_types.h"
#include "..\driverlib/sysctl.h"
#include "..\driverlib/gpio.h"
#include "..\driverlib/pwm.h"
#include "os.h"
#include "motor.h"


//********************************Motor_Init***********************************
//	Configures the PWMs used for the motors.
//! \param None
//! \return None
//*****************************************************************************
void Motor_Init(){
	// PWM initialization
  	SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM);
  	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

  	GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1 );
  	GPIOPinConfigure(GPIO_PF0_PWM0);
  	GPIOPinConfigure(GPIO_PF1_PWM1);
  	
	GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1);
  	SysCtlPWMClockSet( SYSCTL_PWMDIV_32 );
  
  	PWMGenConfigure(PWM_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC | PWM_GEN_MODE_DBG_RUN);
  	PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, 1000 );   //50mhz clock--22ms period

  	PWMOutputInvert(PWM_BASE, (PWM_OUT_0_BIT | PWM_OUT_1_BIT), true);
  
  	PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 500 );
  	PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 500 );
 
  
  	PWMGenEnable(PWM_BASE, PWM_GEN_0);

  	PWMOutputState(PWM_BASE, (PWM_OUT_0_BIT | PWM_OUT_1_BIT), true);

}

//******************************Motor_Drive************************************
//	This is the main function that drives the robot
//! \param Pointer to this thread
//! \return None
//*****************************************************************************
void Motor_Drive(OS_TCB* this)
{    
  	while(1){
  		PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 999 );
  		PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 999 );
  	
		OS_Sleep(this,4000);
  	
		PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 999 );
  		PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 1 );
  	
		OS_Sleep(this,1000);
  	
		PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 999 );
  		PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 999 );
  	
   		OS_Sleep(this,4000);
  	
		PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 1 );
  		PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 999 );
  	
		OS_Sleep(this,1000);  
  	}
}




